package org.timeticker.gui;

import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXStatusBar;
import org.jdesktop.swingx.calendar.JXMonthView;
import org.timeticker.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Main application window
 *
 * @author Ivan Latysh
 * @version 0.1
 * @since 10-Jul-2007 : 12:12:50 AM
 */
public class MainWindow01 extends JFrame{
  // Error prefix MWD00002

  /** Component logger */
  protected Logger logger = Logger.getLogger(this.getClass().getName());
  // main tab pane
  private JTabbedPane tabbedPane = new JTabbedPane();
  /** Activities panel */
//  private JPanel activitiesPanel = new JPanel();
  /** Left side panel */
  private JPanel leftSidePanel = new JPanel(new BorderLayout());
  /** Calendar */
  private JXMonthView monthView = new JXMonthView();
  /** Status bar */
  private JXStatusBar statusBar = new JXStatusBar();
  /** Menu bar */
  private JMenuBar menuBar = new JMenuBar();

  /**
   * Initialize GUI
   */
  public void initUI() {
    this.setLayout(new BorderLayout());

    // setup menu
    JMenu fileMenu = new JMenu("File");
    fileMenu.add(new JMenuItem(closeAppAction));

    // options menu
    JMenu optionsMenu = new JMenu("Options");
    optionsMenu.add(new JMenuItem(workTypesActions));
    optionsMenu.add(new JMenuItem(projectsActions));

    // setup menu
    JMenu helpMenu = new JMenu("Help");
    helpMenu.add(new JMenuItem(aboutAppAction));

    // summon menu
    menuBar.add(fileMenu);
    menuBar.add(optionsMenu);
    menuBar.add(helpMenu);


    // set up month view
    monthView.setTraversable(true);
    monthView.setSelectionMode(JXMonthView.SelectionMode.SINGLE_SELECTION);
    monthView.setPreferredCols(1);
    monthView.setPreferredRows(1);
    monthView.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        if ("selectionChanged".equals(e.getActionCommand())) {
          loadActivityList.actionPerformed(e);
        }
      }
    });

    // set up left side panel
    leftSidePanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    JPanel monthPanel = new JPanel();    
    monthPanel.add(monthView);
    leftSidePanel.add(monthPanel, BorderLayout.NORTH);

    // set up activities panel
//    activitiesPanel.setLayout(new BorderLayout());
//    activitiesPanel.add(leftSidePanel, BorderLayout.WEST);
//    activitiesPanel.add(new JScrollPane(activitiesTable), BorderLayout.CENTER);
//    activitiesPanel.add(toolBar, BorderLayout.NORTH);

    // set up status bar
    statusBar.add(new JLabel("Ready"));    

    // assemble tabbed pane
    tabbedPane.add("", new ActivityListPanel(this));
    // update tab title
    setActivityTabTitle(new Date());
    
    // @todo refactor into a plugin
    // tabbedPane.add("Jira Tasks", new JiraIssuesTaskPanel());

    // Add window listener that will dispose main window on close
    addWindowListener(new WindowListener(){
      public void windowOpened(WindowEvent e) { }
      public void windowClosing(WindowEvent e) {
        closeAppAction.actionPerformed(null);
      }
      public void windowClosed(WindowEvent e) { }
      public void windowIconified(WindowEvent e) { }
      public void windowDeiconified(WindowEvent e) { }
      public void windowActivated(WindowEvent e) { }
      public void windowDeactivated(WindowEvent e) { }
    });

    // main split pane
    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftSidePanel, tabbedPane);

    // set menu bar
    MainWindow01.this.setJMenuBar(menuBar);

    // summon the window
    setTitle("TimeTicker");
    this.getContentPane().add(split, BorderLayout.CENTER);
    this.getContentPane().add(statusBar, BorderLayout.SOUTH);
    
    this.setPreferredSize(new Dimension(800, 600));
    this.pack();
  }

  /**
   * Load activities action
   */
  Action loadActivityList = new AbstractAction("LoadActivitiesForSelectedDate") {
    public void actionPerformed(ActionEvent e) {
    }
  };

  /**
   * Set activity tab title
   *
   * @param date date to use as a title
   */
  private void setActivityTabTitle(Date date) {
    //@todo move date format into config parameters
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    tabbedPane.setTitleAt(0, dateFormat.format(date));
  }

  /**
   * Close application action
   */
  Action closeAppAction = new AbstractAction("Exit") {
    public void actionPerformed(ActionEvent e) {
      // close and dispose the window
      MainWindow01.this.setVisible(false);
      MainWindow01.this.dispose();
      // destroy application
      Main.main.detroyApplication();
    }
  };

  /**
   * About application action
   */
  Action aboutAppAction = new AbstractAction("About TimeTicker") {
    public void actionPerformed(ActionEvent e) {
      JOptionPane.showMessageDialog(MainWindow01.this, "TimeTicker v0.1");
    }
  };

  /**
   * Work types dialog
   */
  Action workTypesActions = new AbstractAction("Work Types") {
    public void actionPerformed(ActionEvent e) {
      // create work types dialog
      WorkTypesDialog wtypes = new WorkTypesDialog(MainWindow01.this);
      // show dialog
      wtypes.setVisible(true);
      // dispose dialog
      wtypes.dispose();
    }
  };

  /**
   * Projects dialog
   */
  Action projectsActions = new AbstractAction("Projects") {
    public void actionPerformed(ActionEvent e) {
      // create projects dialog
      ProjectsDialog projects = new ProjectsDialog(MainWindow01.this);
      // show dialog
      projects.setVisible(true);
      // dispose dialog
      projects.dispose();
    }
  };

}
